﻿using System.Collections.Generic;
using System;
using System.Text.RegularExpressions;
using LoveMusic.Models;
using System.Xml;
using System.IO;
using GSearch;
namespace LoveMusic.Helpers
{
    public class AddMusicBot
    {
        /// <summary>
        ///  Try to get BillBoard top100 songs for 
        /// </summary>
        public void BillboardTop100()
        {
            string[] part;
            string[] subParts;
            string html = string.Empty;
            string songName = string.Empty;
            string artistName = string.Empty;
            int beginPosition = 1;

            try
            {
                while (beginPosition < 100)
                {
                    string url = "http://www.billboard.com/charts/hot-100?begin=" + beginPosition.ToString() + "&order=position#/charts/hot-100?begin=" + beginPosition.ToString() + "&order=position";
                    html = WebHelper.GetResponseStandard(url);
                    part = html.Split(new string[] { "<div class=\"info\">" }, StringSplitOptions.None);

                    for (int i = 1; i <= 10; i++)
                    {
                        subParts = part[i].Split(new string[] { "<a href=\"" }, StringSplitOptions.None);
                        songName = WebHelper.GetMiddleString(subParts[1], ">", "</a");
                        artistName = WebHelper.GetMiddleString(subParts[2], ">", "</a");
                    }
                    beginPosition = beginPosition + 10;
                }
            }
            catch (Exception ex)
            {
                TraceLog.TimeWrite(ex, "ALLMUSICAPI Similar artists");
            }
        }

        public void AllMusicUpdateArtist()
        {
        //    AllMusicBin allMusicBin = new AllMusicBin();

        //    using (ModelDataContext db = new ModelDataContext())
        //    {
        //        foreach (ARTIST artist in db.GetAllArtists())
        //        {
        //            if (artist.status == null)
        //            {
        //                allMusicBin = AllMusicAPI.SearchRequest(artist.NAME);
        //                if (allMusicBin == null)
        //                    db.SetArtistProbablyNotValidFlag(artist);
        //                else
        //                    db.InsertAllMusicBin(ref allMusicBin,ref artist);
        //            }
        //        }
        //    }
        }

        private class EchoHolder
        {
            public int counter;
            public string artistName;
            public string ID;
            public string hotttnesss;
            public string songName;
            public string description = "One of many of Top 2009 2010 songs list.";
            public int category = 15;
            public int year = 2009;
        }

        public void fixReagge()
        {
            using (ModelDataContext db = new ModelDataContext())
            {
                Category[] categories = db.GetCategories();
                List<string> styles = new List<string>();
                int newCategory = 15;
                foreach (Story story in db.GetAllStoriesForCategoryID(4))
                {
                    if (story.ArtistName != "Bob Marley" && story.ArtistName.ToLower() != "sss")
                    {

                        newCategory = UseAllMusic(WebHelper.RemoveSpecialCharacters(story.ArtistName), categories, ref styles);
                        if (newCategory != 4 && newCategory != 66)
                            story.CategoryID = newCategory;
                    }
                }
                db.Save();
            }
        }

        public void FixClassicall()
        {
            using (ModelDataContext db = new ModelDataContext())
            {
                Category[] categories = db.GetCategories();
                int newCategory = 15;
                foreach (Story story in db.GetAllStoriesForCategoryID(3))
                {
                    newCategory = UseWinamp(story.ArtistName, categories);
                    if (newCategory == 20)
                        story.CategoryID = newCategory;
                }
                db.Save();
            }
        }

        public void FixTrance()
        {
            bool start = false;
            using (ModelDataContext db = new ModelDataContext())
            {
                foreach (Story story in db.GetAllStoriesForCategoryID(17))
                {
                    if (story.ArtistName == "LSD")
                        start = true;
                    if (start)
                        db.DeleteStory(story.ID);
                }
            }
        }

        public void FixTehno()
        {
            using (ModelDataContext db = new ModelDataContext())
            {
                Category[] categories = db.GetCategories();
                List<string> styles = new List<string>();
                int newCategory = 15;
                foreach (Story story in db.GetAllStoriesForCategoryID(1))
                {
                    if (story.ArtistName == "M.I.A.")
                    {
                        newCategory = UseAllMusic(story.ArtistName, categories, ref styles);

                        story.CategoryID = newCategory;

                    }
                }
                db.Save();
            }
        }

        public void AddTranceSongs()
        {
            string file = System.Web.HttpContext.Current.Server.MapPath("~/MailTemplates/goa100.txt");
            string body = System.IO.File.ReadAllText(file);
            body = WebHelper.RemoveDigits(body);
            string[] subParts = body.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            for (int i = 0; i < subParts.Length; i++)
            {
                try
                {
                    subParts[i] = subParts[i].Trim();
                    string[] parts = subParts[i].Split(new string[] { " - " }, StringSplitOptions.None);

                    string artistName = parts[0];
                    string songName = parts[1].Replace("\"", "");
                    SubmitBot(songName, 17, "Top trance songs.", artistName);
                }
                catch (Exception ex)
                {
                    continue;
                }
            }
        }

        public void FixTranceTehno()
        {
            using (ModelDataContext db = new ModelDataContext())
            {
                Category[] categories = db.GetCategories();
                List<string> styles = new List<string>();
                int newCategory = 15;
                foreach (Story story in db.GetAllStoriesForCategoryID(15))
                {
                    newCategory = UseAllMusic(story.ArtistName, categories, ref styles);
                    story.CategoryID = newCategory;

                }
                db.Save();
            }
        }
        public void AddClubSongs()
        {
            string file = System.Web.HttpContext.Current.Server.MapPath("~/MailTemplates/club.txt");
            string body = System.IO.File.ReadAllText(file);
            body = WebHelper.RemoveDigits(body);
            string[] subParts = body.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            for (int i = 0; i < subParts.Length; i++)
            {

                try
                {
                    subParts[i] = subParts[i].Trim();
                    subParts[i] = subParts[i].Substring(2, subParts[i].Length - 2);
                    string[] parts = subParts[i].Split(new string[] { " - " }, StringSplitOptions.None);

                    string artistName = parts[0];
                    string songName = parts[1].Replace("\"", "");


                    SubmitBot(songName, 6, "Awesome club song", artistName);
                }
                catch (Exception ex)
                {
                    continue;
                }
            }
        }

        public void AddChillOutSongs()
        {
            string file = System.Web.HttpContext.Current.Server.MapPath("~/MailTemplates/chillout.txt");
            string body = System.IO.File.ReadAllText(file);
            bool start = false;
            body = WebHelper.RemoveDigits(body);
            string[] subParts = body.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            for (int i = 0; i < subParts.Length; i++)
            {

                try
                {
                    subParts[i] = subParts[i].Trim();
                    subParts[i] = subParts[i].Substring(2, subParts[i].Length - 2);
                    string[] parts = subParts[i].Split(new string[] { " - " }, StringSplitOptions.None);

                    string artistName = parts[0];
                    string songName = parts[1].Replace("\"", "");
                    if (artistName == "Andreas Agiannitopoulos")
                        start = true;

                    if (start)
                        SubmitBot(songName, 19, "Cafe del Mar all songs", artistName);
                }
                catch (Exception ex)
                {
                    continue;
                }
            }
        }

        public void Add15000Songs()
        {
            List<EchoHolder> list1000songs = new List<EchoHolder>();
            List<EchoHolder> list15000songs = new List<EchoHolder>();

            #region Get1000 artist

            Uri uri = new Uri("http://developer.echonest.com/api/get_top_hottt_artists?api_key=GZWEIGV8SGDQMITRF&rows=1000&version=3");
            string xml = WebHelper.GetResponseStandard(uri.ToString());
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(new StringReader(xml));
            int counter = 0;
            bool areWeThereYet = false;
            foreach (XmlElement element in xmlDoc.GetElementsByTagName("artist"))
            {
                if (element.GetElementsByTagName("name").Item(0).InnerXml.IndexOf("Eddie Vedder") != -1)
                {
                    areWeThereYet = true;
                }
                if (areWeThereYet)
                {
                    EchoHolder echoHolder = new EchoHolder();
                    echoHolder.artistName = element.GetElementsByTagName("name").Item(0).InnerXml;

                    echoHolder.ID = element.GetElementsByTagName("id").Item(0).InnerXml;
                    echoHolder.hotttnesss = element.GetElementsByTagName("hotttnesss").Item(0).InnerXml;
                    echoHolder.counter = ++counter;
                    if (!list15000songs.Contains(echoHolder))
                        list1000songs.Add(echoHolder);
                }
            }

            #endregion Get1000

            //get audio t.e. song name

            #region Get15000 songs

            counter = 0;
            foreach (EchoHolder echoHolder in list1000songs)
            {
                try
                {
                    uri = new Uri("http://developer.echonest.com/api/get_audio?api_key=GZWEIGV8SGDQMITRF&id=" +
                        echoHolder.ID
                    + "&rows=15&version=3");
                    xml = WebHelper.GetResponseStandard(uri.ToString());
                    xmlDoc = new XmlDocument();
                    xmlDoc.Load(new StringReader(xml));
                }
                catch (Exception ex)
                {
                    continue;
                }
                foreach (XmlElement element in xmlDoc.GetElementsByTagName("doc"))
                {
                    EchoHolder eco = new EchoHolder();
                    eco.artistName = echoHolder.artistName;
                    eco.ID = echoHolder.ID;
                    eco.hotttnesss = echoHolder.hotttnesss;
                    try
                    {
                        eco.counter = ++counter;
                        eco.songName = element.GetElementsByTagName("title").Item(0).InnerXml;
                        if (!list15000songs.Contains(eco))
                            list15000songs.Add(eco);
                    }
                    catch (Exception ex)
                    {
                        TraceLog.TimeWrite(ex, "No title=" + eco.ID);
                    }
                }
                if (list15000songs.Count > 5000)
                {
                    TraceLog.TimeWrite("Last Artist=" + echoHolder.artistName + "id=" + echoHolder.counter);
                    break;
                }

            }

            #endregion Get15000 songs

            #region add categories

            Category[] categories;
            bool categoryFound = false;
            using (ModelDataContext db = new ModelDataContext())
            {
                categories = db.GetCategories();
            }

            #endregion

            List<EchoHolder> listDiscogs = new List<EchoHolder>();
            EchoHolder tempEco = null;
            DiscogsAPI.ResponseClasses.ArtistRequest.resp oResp = null;
            DiscogsAPI.ResponseClasses.ReleaseRequest.resp oRespa = null;

            foreach (EchoHolder echoHolder in list15000songs)
            {

                try
                {
                    #region first time

                    if (tempEco == null)
                    {
                        tempEco = list15000songs[0];
                        oResp = DiscogsAPI.Discogs.SearchArtist(echoHolder.artistName);
                        oRespa = DiscogsAPI.Discogs.SearchRelease(oResp.artist.releases[0].id.ToString());

                        if (echoHolder.category == 15)
                        {
                            #region Find category

                            categoryFound = false;

                            if (oRespa.release.styles != null)
                            {
                                foreach (string style in oRespa.release.styles)
                                {
                                    foreach (Category cat in categories)
                                    {
                                        if (style.ToLower().IndexOf(cat.Name.ToLower()) != -1)
                                        {
                                            echoHolder.category = cat.ID;
                                            categoryFound = true;
                                            break;
                                        }
                                    }
                                    if (categoryFound == true)
                                        break;
                                }
                            }

                            if (categoryFound == false && oRespa.release.genres != null)
                            {
                                foreach (string genre in oRespa.release.genres)
                                {
                                    foreach (Category cat in categories)
                                    {
                                        if (genre.ToLower() == cat.Name.ToLower())
                                        {
                                            echoHolder.category = cat.ID;
                                            categoryFound = true;
                                            break;
                                        }
                                    }
                                    if (categoryFound == true)
                                        break;
                                }
                            }
                            #endregion find category
                        }


                        if (oResp.artist.releases[0].yearSpecified)
                            echoHolder.year = oResp.artist.releases[0].year;

                        #region Disco Lista

                        foreach (DiscogsAPI.ResponseClasses.ArtistRequest.release release in oResp.artist.releases)
                        {
                            EchoHolder eco = new EchoHolder();
                            eco.artistName = echoHolder.artistName;
                            eco.category = echoHolder.category;
                            eco.songName = release.title;
                            eco.category = echoHolder.category;
                            if (release.trackinfoSpecified)
                                eco.description = release.trackinfo;
                            if (release.yearSpecified)
                                eco.year = release.year;
                            if (!listDiscogs.Contains(eco))
                                listDiscogs.Add(eco);
                        }

                        #endregion

                        continue;
                    }

                    #endregion first time

                    echoHolder.artistName = echoHolder.artistName.Replace("$", "s");
                    if (tempEco.artistName != echoHolder.artistName)
                    {// new artist
                        try
                        {
                            tempEco = echoHolder;
                            echoHolder.artistName = echoHolder.artistName.Replace("$", "s");
                            int categoryID = UseWinamp(echoHolder.artistName, categories);

                            if (categoryID == 15)
                            {
                                oResp = DiscogsAPI.Discogs.SearchArtist(echoHolder.artistName);
                                if (oResp.artist != null)
                                {
                                    oRespa = DiscogsAPI.Discogs.SearchRelease(oResp.artist.releases[0].id.ToString());

                                    #region Find category

                                    categoryFound = false;

                                    if (oRespa.release.styles != null)
                                    {
                                        foreach (string style in oRespa.release.styles)
                                        {
                                            foreach (Category cat in categories)
                                            {
                                                if (style.ToLower().IndexOf(cat.Name.ToLower()) != -1)
                                                {
                                                    echoHolder.category = cat.ID;
                                                    categoryFound = true;
                                                    break;
                                                }
                                            }
                                            if (categoryFound == true)
                                                break;
                                        }
                                    }

                                    if (categoryFound == false && oRespa.release.genres != null)
                                    {
                                        foreach (string genre in oRespa.release.genres)
                                        {
                                            foreach (Category cat in categories)
                                            {
                                                if (genre.ToLower() == cat.Name.ToLower())
                                                {
                                                    echoHolder.category = cat.ID;
                                                    categoryFound = true;
                                                    break;
                                                }
                                            }
                                            if (categoryFound == true)
                                                break;
                                        }
                                    }
                                    #endregion find category
                                }
                            }
                            else
                                echoHolder.category = categoryID;

                            #region Disco Lista

                            foreach (DiscogsAPI.ResponseClasses.ArtistRequest.release release in oResp.artist.releases)
                            {
                                EchoHolder eco = new EchoHolder();
                                eco.artistName = echoHolder.artistName;
                                eco.category = echoHolder.category;
                                eco.songName = release.title;
                                eco.category = echoHolder.category;
                                if (release.trackinfoSpecified)
                                    eco.description = release.trackinfo;
                                if (release.yearSpecified)
                                    eco.year = release.year;
                                if (!listDiscogs.Contains(eco))
                                    listDiscogs.Add(eco);
                            }

                            #endregion

                            if (oResp.artist.releases[0].yearSpecified)
                                echoHolder.year = oResp.artist.releases[0].year;
                        }
                        catch (Exception ex)
                        {
                            TraceLog.TimeWrite(ex, "artistName=" + echoHolder.artistName);
                        }
                    }
                    else
                    {
                        echoHolder.category = tempEco.category;
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.TimeWrite(ex, "Exception occured on echoHolder.counter=" + echoHolder.counter);
                    throw new ApplicationException(ex.StackTrace);
                }

                try
                {
                    SubmitBot(echoHolder.songName, echoHolder.category, echoHolder.description, echoHolder.artistName);
                    TraceLog.TimeWrite("Successfully aded artist=" + echoHolder.artistName + " counter=" + echoHolder.counter);
                }
                catch (Exception ex)
                {
                    TraceLog.TimeWrite(ex, "Adding song error artist=" + echoHolder.artistName + " counter=" + echoHolder.counter);
                }
            }

        }

        public int UseAllMusic(string artistName, Category[] categories, ref List<String> styles)
        {
            try
            {
                GWebSearch gws = new GWebSearch();
                WebSearchArgs wsa = new WebSearchArgs();
                string html = string.Empty;
                string url = string.Empty;
                string style = string.Empty;
                wsa.Terms = "site:allmusic.com " + artistName;
                GWebResponse resp = gws.Search(wsa);
                styles = new List<string>();
                if (resp.Response.Results.Length > 0)
                {
                    foreach (GWebResults result in resp.Response.Results)
                    {
                        if (result.Title.ToLower().IndexOf("allmusic") != -1)
                        {
                            html = WebHelper.GetResponseStandard(result.UnescapedUrl);
                            string[] part = html.Split(new string[] { "<div id=\"left-sidebar-list\" style=\"padding-bottom:5px;\">" }, StringSplitOptions.None);
                            string[] gernrePart = part[1].Split(new string[] { "<ul><li><a href=\"" }, StringSplitOptions.None);
                            string genre = WebHelper.GetMiddleString(gernrePart[1], ">", "<");

                            string stylesNotParsed = WebHelper.GetMiddleString(part[2], "<ul>", "</ul>");

                            string[] stylesParts = stylesNotParsed.Split(new string[] { "<li>" }, StringSplitOptions.None);
                            for (int i = 1; i < stylesParts.Length; i++)
                            {
                                string oneStyle = WebHelper.GetMiddleString(stylesParts[i], ">", "</a");
                                styles.Add(oneStyle);
                            }
                            switch (genre)
                            {
                                case "Avant-Garde": return 15;
                                case "Blues": return 9;
                                case "Country": return 15;
                                case "Easy Listening": return 20;
                                case "Electronic": return GetTehnoTranceClub(styles);
                                case "Folk": return 15;
                                case "International": return 14;
                                case "Jazz": return 9;
                                case "Latin": return 15;
                                case "New Age": return 19;
                                case "Pop/Rock": return GetPopOrRockOrSka(styles);
                                case "R&B": return 10;
                                case "Rap": return 2;
                                case "Reggae": return 4;
                                case "Stage & Screen": return 19;
                                case "Vocal": return GetPopOrBlues(styles);

                                #region classical

                                case "Classical Genre": return 20;
                                case "Avant-Garde Music": return 20;
                                case "Ballet": return 20;
                                case "Band Music": return 20;
                                case "Chamber Music": return 20;
                                case "Choral": return 20;
                                case "Film Score": return 20;
                                case "Keyboard": return 20;
                                case "Opera": return 20;
                                case "Orchestral": return 20;
                                case "Show/Musical": return 20;
                                case "Symphony": return 20;
                                case "Vocal Music": return 20;

                                #endregion

                                default: return 15;
                            }
                        }
                        else
                            return 66;

                    }
                }
                return 66;
            }
            catch (Exception e)
            {
                return 66;
            }
        }

        private int GetTehnoTranceClub(List<string> styles)
        {
            int numberOfClubs = 0;
            int numberOfTrance = 0;
            int numberOfHouse = 0;
            foreach (string style in styles)
            {
                if (style.ToLower().IndexOf("drum'n'bass") != -1)
                    return 16;
                if (style.ToLower().IndexOf("club") != -1)
                    numberOfClubs++;
                if (style.ToLower().IndexOf("trance") != -1 || style.ToLower().IndexOf("psychedelic") != -1)
                    numberOfTrance++;
                if (style.ToLower().IndexOf("house") != -1)
                    numberOfHouse++;
            }

            if (numberOfHouse >= numberOfTrance)
                return 18;
            else if (numberOfTrance >= numberOfClubs)
                return 17;

            return 7;
        }

        private int GetPopOrBlues(List<string> styles)
        {
            int numberOfPops = 0;
            int numberOfBlues = 0;
            foreach (string style in styles)
            {
                if (style.ToLower().IndexOf("pop") != -1)
                    numberOfPops++;
                else if (style.ToLower().IndexOf("jazz") != -1 || style.ToLower().IndexOf("blues") != -1)
                    numberOfBlues++;
            }
            if (numberOfBlues >= numberOfPops)
                return 9;
            else
                return 3;
        }

        private int GetPopOrRockOrSka(List<string> styles)
        {
            int numberOfRocks = 0;
            int numberOfPops = 0;

            foreach (string style in styles)
            {
                if (style.ToLower().IndexOf("ska") != -1)
                    return 5;
                if (style.ToLower().IndexOf("pop") != -1)
                    numberOfPops++;
                else if (style.ToLower().IndexOf("rock") != -1)
                    numberOfRocks++;
            }
            if (numberOfRocks >= numberOfPops)
                return 1;
            else
                return 3;
        }

        private int UseWinamp(string artistName, Category[] categories)
        {
            try
            {
                artistName = artistName.Trim();
                artistName = artistName.Replace(" ", "-");
                string html = WebHelper.GetResponseStandard(@"http://www.winamp.com/artist/" + artistName);

                string[] strArray = html.Split(new string[] { "<div class=\"author\">" }, StringSplitOptions.None);
                string style = WebHelper.GetMiddleString(strArray[1], "title=\"", "\">");
                TraceLog.TimeWrite("style=" + style);
                foreach (Category cat in categories)
                {
                    if (style.ToLower().IndexOf("easy listening") != -1)
                        return 20;

                    if (style.ToLower().IndexOf("Dance & Electronic") != -1)
                    {
                        return 7;
                    }

                    if (style.ToLower().IndexOf("Dance") != -1)
                    {
                        return 6;
                    }
                    if (style.ToLower().IndexOf("Hip-Hop") != -1)
                    {
                        return 2;
                    }
                    if (style.ToLower().IndexOf("Easy Listening") != -1)
                    {
                        return 19;
                    }

                    if (style.ToLower().IndexOf(cat.Name.ToLower()) != -1)
                    {
                        return cat.ID;
                    }
                }
                return 15;
            }
            catch (Exception e)
            {
                return 15;
            }

        }

        public void AddSka()
        {
            string file = System.Web.HttpContext.Current.Server.MapPath("~/MailTemplates/ska140.txt");
            string body = System.IO.File.ReadAllText(file);
            body = WebHelper.RemoveDigits(body);
            //body = Regex.Replace(body, "\\[([^\\s]*)\\]", "$1");
            // body= System.Text.RegularExpressions.Regex.Replace(body, @"[^\w\.-]", "");

            string[] subParts = body.Split(new string[] { "(:)\r\n." }, StringSplitOptions.None);
            for (int i = 67; i < subParts.Length - 1; i++)
            {
                if (i == 0)
                    subParts[i] = subParts[i].Substring(2, subParts[i].Length - 2);
                else
                    subParts[i] = subParts[i].Substring(1, subParts[i].Length - 1);
                string[] parts = subParts[i].Split(new string[] { "-" }, StringSplitOptions.None);

                string artistName = parts[0].Trim();
                string songName = parts[1].Trim();

                SubmitBot(songName, 5, "Top 140 SKA Songs", artistName);
            }

        }

        public void AddReage()
        {
            string file = System.Web.HttpContext.Current.Server.MapPath("~/MailTemplates/reage200.txt");
            string body = System.IO.File.ReadAllText(file);
            body = WebHelper.RemoveDigits(body);
            string[] subParts = body.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            for (int i = 0; i < subParts.Length; i++)
            {
                subParts[i] = subParts[i].Substring(2, subParts[i].Length - 2);
                string[] parts = subParts[i].Split(new string[] { " - " }, StringSplitOptions.None);

                string songName = parts[0];
                string artistName = parts[1];

                SubmitBot(songName, 4, "Greatest Reggae Songs", artistName);
            }

        }

        public void AddPop()
        {
            string file = System.Web.HttpContext.Current.Server.MapPath("~/MailTemplates/pop100.txt");
            string body = System.IO.File.ReadAllText(file);

            string[] subParts = body.Split(new string[] { "\r\n,\r\n" }, StringSplitOptions.None);

            string songName = string.Empty;
            string artistName = string.Empty;
            SongController songController = new SongController();

            for (int i = 0; i < subParts.Length; i = i + 2)
            {
                subParts[i] = subParts[i].Replace("\r", "");
                subParts[i] = subParts[i].Replace("\n", "");
                subParts[i] = WebHelper.RemoveDigits(subParts[i]);
            }

            for (int i = 154; i < subParts.Length; i = i + 2)
            {
                songName = subParts[i];
                artistName = subParts[i + 1];
                if (i != 142)
                    SubmitBot(songName, 3, "Greatest Pop Songs", artistName);
            }
        }

        public void AddRap()
        {
            string file = System.Web.HttpContext.Current.Server.MapPath("~/MailTemplates/rap100.txt");
            string body = System.IO.File.ReadAllText(file);

            string[] subParts = body.Split(new string[] { "\r\n,\r\n" }, StringSplitOptions.None);

            string songName = string.Empty;
            string artistName = string.Empty;
            SongController songController = new SongController();

            for (int i = 0; i < subParts.Length; i = i + 2)
            {
                subParts[i] = subParts[i].Replace("\r", "");
                subParts[i] = subParts[i].Replace("\n", "");
                subParts[i] = WebHelper.RemoveDigits(subParts[i]);
            }

            for (int i = 0; i < subParts.Length; i = i + 2)
            {
                songName = subParts[i];
                artistName = subParts[i + 1];
                SubmitBot(songName, 2, "Greatest Rap Hip Hop Songs", artistName);
            }
        }

        public void AddRock()
        {
            string file = System.Web.HttpContext.Current.Server.MapPath("~/MailTemplates/rock300.txt");
            string body = System.IO.File.ReadAllText(file);
            body = WebHelper.RemoveDigits(body);
            string[] subParts = body.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            for (int i = 0; i < subParts.Length; i++)
            {
                subParts[i] = subParts[i].Substring(2, subParts[i].Length - 2);
                string[] parts = subParts[i].Split(new string[] { " - " }, StringSplitOptions.None);

                string songName = parts[0];
                string artistName = parts[1];

                SubmitBot(songName, 1, "Greatest Rock Songs", artistName);
            }

        }

        public void AddBlues()
        {
            string file = System.Web.HttpContext.Current.Server.MapPath("~/MailTemplates/blues100.txt");
            string body = System.IO.File.ReadAllText(file);

            string[] subParts = body.Split(new string[] { "\r\n,\r\n" }, StringSplitOptions.None);

            string songName = string.Empty;
            string artistName = string.Empty;
            SongController songController = new SongController();

            for (int i = 0; i < subParts.Length; i = i + 2)
            {
                subParts[i] = subParts[i].Replace("\r", "");
                subParts[i] = subParts[i].Replace("\n", "");
                subParts[i] = WebHelper.RemoveDigits(subParts[i]);

            }

            for (int i = 0; i < subParts.Length; i = i + 2)
            {
                songName = subParts[i];
                artistName = subParts[i + 1];
                SubmitBot(songName, 9, "Greatest Blues Songs", artistName);
            }
        }

        public void SubmitBot(string songTitle, int storyCategoryId, string storyDescription, string artistName)
        {
            string encodingCode = string.Empty;
            string tags = string.Empty;
            string videoID = string.Empty;
            string imageURL = string.Empty;
            string youTubeDescrption = string.Empty;
            bool hasImage = false;


            using (ModelDataContext db = new ModelDataContext())
            {
                try
                {
                    YouTubeApi youTube = new YouTubeApi();
                    Mp3FileApi mp3FileApi = new Mp3FileApi();
                    youTube.GetYouTubeVideo(songTitle, artistName, ref encodingCode, ref videoID, ref tags, ref youTubeDescrption);

                    //string[] tagz = tags.Split(new string[] { "," }, StringSplitOptions.None);
                    //foreach (string tagce in tagz)
                    //{
                    //    tag
                    //}  Who I Am
                    if (!db.StoryExists(songTitle, artistName))
                        db.SubmitStoryBot(songTitle, artistName, storyCategoryId, storyDescription, tags, mp3FileApi.GetMp3Urls(artistName, songTitle), videoID, new Guid("1685CC87-BA2F-450B-BBE6-2F81A0F149A9"), youTubeDescrption);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(ex.Message);
                }
            }

        }

      

        public void AddDNB()
        {
            string file = System.Web.HttpContext.Current.Server.MapPath("~/MailTemplates/dnb.txt");
            string body = System.IO.File.ReadAllText(file);
            body = WebHelper.RemoveDigits(body);
            string[] subParts = body.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            for (int i = 0; i < subParts.Length; i++)
            {
                subParts[i] = subParts[i].Substring(1, subParts[i].Length - 1);
                string[] parts = subParts[i].Split(new string[] { " - " }, StringSplitOptions.None);

                string artistName = parts[0];
                string songName = parts[1];

                SubmitBot(songName, 16, "Need some new Drum and Bass songs. :)?", artistName);
            }
        }




    }
}